//
//  FANEmoticonViewModel.m
//  Weibo - OC
//
//  Created by user on 16/4/27.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANEmoticonViewModel.h"

@interface FANEmoticonViewModel ()

@property (nonatomic, strong) NSMutableArray *recentArray;

@property (nonatomic, strong) NSArray *defaultsArray;

@property (nonatomic, strong) NSArray *emojiArray;

@property (nonatomic, strong) NSArray *lxhArray;

@end

@implementation FANEmoticonViewModel

- (NSArray *)lxhArray{
    
    if (!_lxhArray) {
        
        _lxhArray = [NSArray array];
    }
    return _lxhArray;
}

- (NSArray *)emojiArray{
    
    if (!_emojiArray) {
        
        _emojiArray = [NSArray array];
        
    }
    return _emojiArray;
}

- (NSArray *)defaultsArray{
    
    if (!_defaultsArray) {
        
        _defaultsArray = [NSArray array];
    }
    
    return _defaultsArray;
}

- (NSMutableArray *)recentArray{
    
     NSString *path = [(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).lastObject) stringByAppendingPathComponent:@"emoticon.archive"];
    
    _recentArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (_recentArray != nil) {
        
//        FANEmoticon *emoticon = [[FANEmoticon alloc] init];
        
        return _recentArray;
        
    }else{
        
        _recentArray = [NSMutableArray array];

    }
    
    
    return _recentArray;
}

- (NSBundle *)bundle{
    
    if (!_bundle) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Emoticons.bundle" ofType:nil];
        
        _bundle = [NSBundle bundleWithPath:path];
    }
    
    return _bundle;
}

- (FANEmoticon *)emoticonWithChs:(NSString *)chs{
    
    for (FANEmoticon *value in [self getEmoticonsArray:@"default"]) {
        
        if ([value.chs isEqualToString:chs]) {
            
            return value;
            
        }
    }
    
    for (FANEmoticon *value in [self getEmoticonsArray:@"lxh"]) {
        
        if ([value.chs isEqualToString:chs]) {
            
            return value;
        }
    }
    
    return nil;
}

- (void)saveRecentEmoticon:(FANEmoticon *)emoticon{
    
//    NSLog(@"%@",emoticon.chs);
    NSMutableArray *emoticons = [NSMutableArray arrayWithArray:self.allEmoticons[0][0]];
    
    NSMutableArray *emoticonArray = [NSMutableArray array];
    
    [emoticons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        FANEmoticon *emoticonObj = (FANEmoticon *)obj;
//        NSLog(@"%@",emoticonObj.chs);
        
        
        if ([emoticon.type isEqualToString:emoticonObj.type]) {
            
            if ([emoticon.type isEqualToString:@"1"]) {
                
                if ([emoticon.code isEqualToString:emoticonObj.code]) {
                    
                    [emoticonArray addObject:emoticonObj];
                    
                }
            }else{
                
                if ([emoticon.chs isEqualToString:emoticonObj.chs]) {
                    
                    [emoticonArray addObject:emoticonObj];
                    
                }
            }
            
            
        }
   
        
    }];
    
    
    for (FANEmoticon *emoticon in emoticonArray) {
        
        NSUInteger index = [emoticons indexOfObject:emoticon];
        
        [emoticons removeObjectAtIndex:index];
        
    }
    
    
    [emoticons insertObject:emoticon atIndex:0];
    
    while (emoticons.count > 20) {
        
        [emoticons removeLastObject];
        
    }
    
    self.recentArray = emoticons;
    
    NSString *path = [(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).lastObject) stringByAppendingPathComponent:@"emoticon.archive"];
    
    [NSKeyedArchiver archiveRootObject:emoticons toFile:path];
    
//    self.allEmoticons[0][0] = emoticons;
    
}

- (NSMutableArray *)allEmoticons{
    
//    FANEmoticon *emoticon = [[FANEmoticon alloc] init];
    
//    NSMutableArray *recentArray = [NSMutableArray arrayWithObject:emoticon];
    
//    NSArray *recentArray = @[emoticon];
    
    self.defaultsArray = [self getEmoticonsArray:@"default"];
    
    self.emojiArray = [self getEmoticonsArray:@"emoji"];
    
    self.lxhArray = [self getEmoticonsArray:@"lxh"];
    
    NSArray *res =  @[
             
//             [self getSectionArray:recentArray],
             [NSMutableArray arrayWithArray:@[self.recentArray]],
             [self getSectionArray:self.defaultsArray],
             [self getSectionArray:self.emojiArray],
             [self getSectionArray:self.lxhArray]
             
             ];
    
    return [NSMutableArray arrayWithArray:res];
}

+ (instancetype)sharedEmoticon{
    
    static FANEmoticonViewModel *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[FANEmoticonViewModel alloc] init];
    });
    
    return instance;
}


- (NSMutableArray *)getSectionArray:(NSArray *)emoticonsArray{
    
    NSInteger pageCount = (emoticonsArray.count - 1)/20 + 1;
    
    NSMutableArray *sectionMutable = [NSMutableArray array];
    
    for (int i = 0; i < pageCount; i++) {
        
        
        NSUInteger len = 20;
        
        NSUInteger loc = i * 20;
        
        if (emoticonsArray.count < loc + len) {
            
            len = emoticonsArray.count - loc;
            
        }
        
        NSArray *tempArray = [emoticonsArray subarrayWithRange:NSMakeRange(loc, len)];
        
        [sectionMutable addObject:tempArray];
        
    }
    
    return sectionMutable;
    
}

- (NSArray *)getEmoticonsArray:(NSString *)filePath{
    
    NSString *path = [[FANEmoticonViewModel sharedEmoticon].bundle pathForResource:[NSString stringWithFormat:@"%@/info.plist",filePath] ofType:nil];
    
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray *mutable = [NSMutableArray array];
    
    for (NSDictionary *dict in tempArray) {
        
        FANEmoticon *model = [FANEmoticon modelWithDict:dict];
        
        model.folderName = filePath;
        
        [mutable addObject:model];
    }
    
    return mutable;
}

@end
